package com.itheima.heimademo01.controller;

import com.itheima.heimademo01.base.response.Result;
import com.itheima.heimademo01.pojo.entity.Category;
import com.itheima.heimademo01.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Validated
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 新增文章分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result category(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.category(category);
        return Result.success();
    }

    /**
     * 文章分类列表
     *
     * @return
     */
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> cs = categoryService.list();
        return Result.success(cs);
    }

    /**
     * 获取文章分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Result<Category> detail(@RequestParam Integer id) {
        Category c = categoryService.detail(id);
        return Result.success(c);
    }


    /**
     * 更新文章分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        categoryService.delete(id);
        return Result.success();
    }
}
